define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {

        index: function () {

            var html = '<div class="form-group">';
            html += '<label class="control-label col-xs-12 col-sm-6">请选择下载格式:</label>';
            html += '</div>';
            html += '<div class="form-group">';
            html += '<label class="control-label col-xs-12 col-sm-2"></label>';

            html += ' <div class="col-xs-12 col-sm-8">';
            html += ' <div class="radio">';
            html += '<label for="type-word"><input id="type-word" name="type" type="radio" value="word" checked /> Word文档</label>'
            html += '<label for="type-html"><input id="type-html" name="type" type="radio" value="html" /> HTML 文件</label>'
            html += '</div>';
            html += '</div>';
            html += '</div>';


            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilujob.job_apply/index' + location.search,
                    detail_url: 'xilujob.resume/detail',
                    invite_url: 'xilujob.job_interview/create_interview',
                    del_url: '',
                    table: 'xilujob_resume',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                search: false, //是否启用快速搜索
                commonSearch: true, //是否启用通用搜索
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false,visible:false},
                        {field: 'job_id', title: __('职位'),searchList:Config.jobs, formatter: Table.api.formatter.normal},
                        {field: 'resume.status', title: __('简历状态'), visible:false,searchList:{1:__('公开'),0:__('未公开')}},
                        {field: 'person',title:'基本信息',align:'left',formatter: Controller.api.formatter.personal , operate: false},
                        {field: 'work_experience', title: __('工作经验'),align:'left', formatter: Controller.api.formatter.experience, operate:false},
                        //{field: 'status', title: __('应聘状态'), searchList:{0:__('投递待处理'),1:__('投递拒绝'),2:__('面试邀请待回应'),3:__('拒绝面试邀请'),4:__('同意面试邀请'),5:__('面试失败'),6:__('面试录取'),7:__('面试录取被拒绝')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('投递时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('应聘状态'), searchList:{0:__('投递待处理'),1:__('投递拒绝'),2:__('已邀请面试')}, formatter: Table.api.formatter.normal},

                        {field: 'operate', title: __('Operate'),buttons: [
                                {
                                    name: 'detail',
                                    title: '详情',
                                    text: '详情',
                                    icon: '',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.detail_url+'?ids='+row.resume_id+'&apply_id='+row.id
                                    }
                                },
                                {
                                    name: 'interview',
                                    title: '面试邀请',
                                    text: '面试邀请',
                                    icon: '',
                                    classname: 'btn btn-xs btn-success btn-click',
                                    click: function (option,row,button) {
                                        Fast.api.ajax({
                                            url: Fast.api.fixurl('xilujob.resume/action_msg'),
                                            data:{type:'interview',resume_id:row.id}
                                        },function () {
                                            Fast.api.open($.fn.bootstrapTable.defaults.extend.invite_url+'?resume_id='+row.resume_id+'&apply_id='+row.id,'面试邀请',{area:['578px','90%']});
                                            return false;
                                        }, function (data, ret) {
                                            Layer.confirm(ret.msg, {
                                                btn: ['立即购买','稍后再说']
                                            },function (index) {
                                                location.href = Fast.api.fixurl("xilujob.package/index");
                                            });
                                            return false;
                                        });
                                    }
                                },
                                {
                                    name: 'download',
                                    title: '下载简历',
                                    text: '下载简历',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: '',
                                    click: function (option,row,button) {
                                        Fast.api.ajax({
                                            url: Fast.api.fixurl('xilujob.resume/action_msg'),
                                            data:{type:'resume_view',resume_id:row.resume_id}
                                        },function () {
                                            layer.open({
                                                //formType: 2,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                                                title: '下载简历',
                                                area: ['400px', '240px'],
                                                btnAlign: 'c',
                                                closeBtn:'1',//右上角的关闭
                                                content: html,
                                                btn:['确认','关闭'],
                                                yes: function (index, layero) {
                                                    var type = $(":checked[name=type]").val();
                                                    if(!row.id){
                                                        Toastr.error('未找到简历id');
                                                        return false;
                                                    }
                                                    location.href = Fast.api.fixurl('xilujob.resume/download?type='+type+'&ids='+row.resume_id)
                                                    return false;
                                                },
                                            });
                                            return false;
                                        }, function (data, ret) {
                                            Layer.confirm(ret.msg, {
                                                btn: ['立即购买','稍后再说']
                                            },function (index) {
                                                location.href = Fast.api.fixurl("xilujob.package/index");
                                            });
                                            return false;
                                        });
                                    },
                                },
                                {
                                    name: 'apply',
                                    title: '不合适',
                                    text: '不合适',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: '',
                                    click: function (option,row,button) {
                                        Layer.confirm('确认不合适？', function(index) {
                                            Fast.api.ajax({
                                                url: 'xilujob.job_apply/reject_apply?ids='+row.id, data: {}
                                            }, function(data,ret) {
                                                $(".btn-refresh").trigger("click");
                                            });
                                            Layer.close(index);
                                        });
                                    },
                                    visible:function (row) {
                                        return row.status == 0;
                                    }
                                },
                            ], table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            var resumeIds = [];
            table.on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table", function (a, i) {
                "check" == a.type || "uncheck" == a.type ? i = [i] : resumeIds = [], $.each(i, function (e, i) {
                    if (a.type.indexOf("uncheck") > -1) {
                        var r = resumeIds.indexOf(i.resume.id);
                        r > -1 && resumeIds.splice(r, 1);
                    } else -1 == resumeIds.indexOf(i.resume.id) && resumeIds.push(i.resume.id);
                })
            })
            // 为表格绑定事件
            Table.api.bindevent(table);

            //批量邀请
            $('.batch-invite').on('click', function () {
                var apply_ids = Table.api.selectedids(table);
                //检测次数
                Fast.api.ajax({
                    url: Fast.api.fixurl('xilujob.resume/action_msg'),
                    data:{type:'interview',resume_ids:resumeIds.join(',')}
                },function () {
                    Fast.api.open(Fast.api.fixurl("xilujob.job_interview/batch_invite")+'?apply_ids='+apply_ids.join(',')+'&resume_ids='+resumeIds.join(','),'面试邀请',{area:['578px','90%']});
                    return false;
                }, function (data, ret) {
                    Layer.confirm(ret.msg, {
                        btn: ['立即购买','稍后再说']
                    },function (index) {
                        location.href = Fast.api.fixurl("xilujob.package/index");
                    });
                    return false;
                });
            });

            //批量下载简历
            $('.batch-download').on('click', function () {
                //检测次数
                Fast.api.ajax({
                    url: 'xilujob.resume/action_msg',
                    data: {type:'resume_view',resume_ids: resumeIds.join(',')}
                }, function (data, ret) {
                    layer.open({
                        //formType: 2,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                        title: '下载简历',
                        area: ['600px', '400px'],
                        btnAlign: 'c',
                        closeBtn:'1',//右上角的关闭
                        content: html,
                        btn:['确认','关闭'],
                        yes: function (index, layero) {
                            var type = $(":checked[name=type]").val();
                            location.href = Fast.api.fixurl('xilujob.resume/batch_download?type='+type+'&ids='+resumeIds.join(','))
                            return false;
                        }
                    });
                    return false;
                }, function (data, ret) {
                    Layer.confirm(ret.msg, {
                        btn: ['立即购买','稍后再说']
                    },function (index) {
                        location.href = Fast.api.fixurl("xilujob.package/index");
                    });
                    return false;
                });
            });

            //批量不合适
            $('.batch-unfit').on('click', function () {
                var selects = $('#table').bootstrapTable('getAllSelections');
                var ids = [];
                for (var i = 0; i < selects.length; i++) {
                    ids.push(selects[i].id);
                }
                Layer.confirm('确认不合适？', function (index) {
                    Fast.api.ajax({
                        url: 'xilujob.job_apply/reject_apply',
                        data: {ids: ids.join(',')}
                    }, function (data, ret) {
                        $(".btn-refresh").trigger("click");
                    });
                    Layer.close(index);
                    return false;
                });

            });
        },
        api: {
            formatter:{
                personal: function(value,row) {
                    var resume_data = row.resume.personal.age+' | '
                        + (row.resume.personal.work_experience ? row.resume.personal.work_experience.name : '') + ' | '
                        + (row.resume.personal.education_level ? row.resume.personal.education_level.name : '') + ' | '
                        + (row.resume.personal.current_state ? row.resume.personal.current_state.name : '');
                    var summary_content = row.resume.summary.content;
                    summary_content = summary_content.length>20 ? summary_content.substring(0,20)+'...' : summary_content;
                    var html = '<div class="media">';
                    html += '<a class="media-left" href="#">';
                    html += '<img width="50px" height="50px" src="'+Fast.api.cdnurl(row.resume.personal.avatar)+'" class="img-circle" alt="">';
                    // html += '<div>'+row.resume.job_intention.expect_salary.name+'</div>';
                    html += '</a>';

                    html += '<div class="media-body" style="width: auto">';
                    //html += '<a href="'+Fast.api.fixurl('xilujob.resume/detail?ids='+row.resume.id+'&apply_id='+row.id)+'" class="btn-dialog" data-title="简历详情"><h4>'+row.resume.personal.realname+'</h4>';
                    html += '<h4>'+row.resume.personal.realname+'</h4>';
                    html += '<p>'+resume_data+'</p>';
                    // html += '<p>求职期望：'+row.resume.job_intention.fav_city.allname+'</p>';
                    html += '<p><a title="'+row.resume.summary.content+'" style="width: 150px">'+summary_content+'</a></p>';
                    html += '</div>';
                    html += '</div>';
                    return html;
                },
                experience: function(value,row){
                    if(!row.resume.works) return '';
                    var html = '';
                    for (var i=0;i<row.resume.works.length;i++){
                        var end = row.resume.works[i].end ? row.resume.works[i].end : "至今";
                        html += '<h5>'+row.resume.works[i].start+'-'+end+'   '+row.resume.works[i].company_name+' . '+row.resume.works[i].job_title+'</h5>';
                    }
                    return html;
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }

    };
    return Controller;
});